from django.shortcuts import render
from www import models
from utils.search_group import SearchGroup,Option


class MyOption(Option):
    def get_queryset(self, related_model, request):
        return related_model.objects.filter(**self.db_condition)


def demo(request):
    # 只支持FK组件："level"  "creator"
    group = SearchGroup(models.Customer, request,
                        Option('mobile', is_multi=False, is_choice=False, db_condition={'id__lte': 11},
                               text_func=lambda x: x.mobile, value_func=lambda x: x.mobile),
                        Option('level', is_multi=False, db_condition={'active': 1}),
                        Option('creator', is_multi=True, ),
                        Option('gender', is_multi=False, is_choice=True, text_func=lambda x: str(x[1]),
                               value_func=lambda x: str(x[0])),
                        Option('hbs', is_multi=True)
                        )
    row_list = group.get_row_list()
    condition = group.get_condition()
    queryset = models.Customer.objects.filter(**condition).order_by('-id')
    # pager = Pagination(request, queryset)
    return render(request, 'v2.html', {"queryset": queryset, "row_list": row_list})
